package api;

import model.User;
import model.UserDao;
import org.w3c.dom.html.HTMLOptGroupElement;
import view.HtmlGenerator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 登录 服务
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        if(name == null || "".equals(name) || password == null || "".equals(password)){
            String html = HtmlGenerator.getMessagePage("用户名或密码为空!","login.html");
            resp.getWriter().write(html);
            return ;
        }
        // 数据校验
        UserDao userDao = new UserDao();
        User user = userDao.selectByName(name);
        if(user == null || !password.equals(user.getPassword())){
            String html = HtmlGenerator.getMessagePage("用户名或密码错误","login.html");
            resp.getWriter().write(html);
            return ;
        }

        // 匹配成功 则认为登录成功,创建一个session;
        HttpSession httpSession = req.getSession(true);
        httpSession.setAttribute("user",user);
        // 返回一个登录成功的提示页面;
        String html = HtmlGenerator.getMessagePage("登陆成功","article");
        resp.getWriter().write(html);
    }
}
